package com.jrh.web.admin;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jrh.biz.img.ImgUploadService;
import com.jrh.biz.mall.MallService;
import com.jrh.domain.entity.Mall;
import com.jrh.web.admin.form.AdminFormCopier;
import com.jrh.web.admin.form.AdminMallForm;

/**
 * 类MallController.java的实现描述：卖场
 * 
 * @author mmyjse@gmail.com 2012-7-25 上午1:05:42
 */
@Controller
@RequestMapping("/admin/mall")
public class AdminMallController {

    @Autowired
    private MallService      mallService;

    @Autowired
    private ImgUploadService imgUploadService;

    @RequestMapping(method = RequestMethod.GET, value = "/list")
    public String list(Model model) {
        List<Mall> mallList = mallService.findAll();
        model.addAttribute("datas", mallList);
        return "admin/mall/list";
    }

    @RequestMapping(method = RequestMethod.GET, value = "/update/{id}")
    public String update(String success, @PathVariable Long id, Model model) {
        Mall mall = mallService.loadById(id);
        model.addAttribute("data", mall);
        model.addAttribute("success", Boolean.valueOf(success));
        return "admin/mall/update";
    }

    @RequestMapping(method = RequestMethod.POST, value = "/update")
    public String doUpdate(@ModelAttribute("mall") AdminMallForm mallForm) {
        if (mallForm != null && mallForm.getLogoImage() != null && mallForm.getLogoImage().getSize() > 0) {
            String logoUrl = imgUploadService.upload(mallForm.getLogoImage(), "mall");
            mallForm.setLogoUrl(logoUrl);
        }
        Mall mall = AdminFormCopier.convert(mallForm, null);
        boolean isSuccess = mallService.updateMall(mall);
        return "redirect:/admin/mall/update/" + mall.getId() + ".htm?success=" + isSuccess;
    }

    @RequestMapping(method = RequestMethod.POST, value = "/add")
    public String doAdd(@ModelAttribute("mall") AdminMallForm mallForm) {
        String logoUrl = imgUploadService.upload(mallForm.getLogoImage(), "mall");
        mallForm.setLogoUrl(logoUrl);
        Mall mall = AdminFormCopier.convert(mallForm, null);
        boolean isSuccess = mallService.addMall(mall);
        if (isSuccess) {
            return "redirect:/admin/mall/list";
        } else {
            return "/admin/error";
        }
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/delete/{id}")
    public String delete(@PathVariable Long id) {
        boolean isSuccess = mallService.deleteMallBy(id);
        if (isSuccess) {
            return Boolean.TRUE.toString();
        } else {
            return Boolean.FALSE.toString();
        }
    }

}
